#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@desc: .

@Project：kuberCloudTest 
@File：publicnetwork.py
@Author：admin
@Date：2024/4/11 17:00 
"""
from typing import List

from kuberCloudTest.common.basic import basicUtils
from kuberCloudTest.common.constant import OFFSET, LIMIT, AsyncType
from kuberCloudTest.common.utils import _getOperationType
from kuberCloudTest.networks.urls import networkUrls


class publicNetworkUtils(basicUtils):

    def __init__(self, accessToken):
        super().__init__(accessToken)


def get_utils(accessToken):
    return publicNetworkUtils(accessToken=accessToken)


def getPublicNetwork(accessToken, **kwargs):
    """
    获取公共网络
    :param accessToken:
    :param kwargs: limit:
                   offset:
                   keyword: xxx
                   searchItem: 0: 公有网络名称; 1: 公有网络uuid
    :return:
    """
    return get_utils(accessToken).cloud_http_request.get(networkUrls.getPublicNetwork.value,
                                                         {
                                                             'offset': kwargs.get('offset', OFFSET),
                                                             'limit': kwargs.get('limit', LIMIT),
                                                             'keyword': kwargs.get('keyword', ''),
                                                             'searchitem': kwargs.get('searchItem', 0),
                                                         })


def createPublicNetwork(accessToken, **kwargs):
    """
    创建公有网络
    :param accessToken:
    :param kwargs: name: xxx
                   description: xxx
                   l2NetworkUuid: xxx
    :return:
    """
    pass


def updatePublicNetwork(accessToken, publicNetworkId, name, remark):
    """
    更新公有网络
    :param accessToken:
    :param publicNetworkId: 公有网络UUID
    :param name: 公有网络名称
    :param remark: 描述
    :return:
    """
    operationType = 5202
    data = _getOperationType(operationType, AsyncType.ASYNC.value)
    data['pnname'] = name
    data['remark'] = remark
    data['nid'] = publicNetworkId

    return get_utils(accessToken).cloud_http_request.post(networkUrls.updatePublicNetwork.value, data=data)


def deletePublicNetwork(accessToken, publicNetworkIds: List[str]):
    """
    删除公有网络
    :param accessToken:
    :param publicNetworkIds: 公有网络UUID(数组)
    :return:
    """
    operationType = 5203
    data = _getOperationType(operationType, AsyncType.ASYNC.value)
    data['ids'] = publicNetworkIds

    return get_utils(accessToken).cloud_http_request.post(networkUrls.deletePublicNetwork.value, data=data)


def globalSharePublicNetwork(accessToken, publicNetworkIds):
    """
    全局共享公有网络
    :param accessToken:
    :param publicNetworkIds: 公有网络UUID(多个使用英文逗号分割)
    :return:
    """
    operationType = 5211
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['ids'] = publicNetworkIds

    return get_utils(accessToken).cloud_http_request.post(networkUrls.globalSharePublicNetwork.value, data=data)


def globalRecallPublicNetwork(accessToken, publicNetworkIds):
    """
    全局撤回公有网络
    :param accessToken:
    :param publicNetworkIds: 公有网络UUID(多个使用英文逗号分割)
    :return:
    """
    operationType = 5212
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['ids'] = publicNetworkIds

    return get_utils(accessToken).cloud_http_request.post(networkUrls.globalRecallPublicNetwork.value, data=data)


def updatePublicNetworkMTU(accessToken, publicNetworkId, mtu):
    """
    更新公有网络MTU
    :param accessToken:
    :param publicNetworkId: 公有网络UUID
    :param mtu: MTU
    :return:
    """
    operationType = 5210
    data = _getOperationType(operationType, AsyncType.ASYNC.value)
    data['nid'] = publicNetworkId
    data['mtu'] = mtu

    return get_utils(accessToken).cloud_http_request.post(networkUrls.updatePublicNetworkMTU.value, data=data)


def loadVMByPublicNetwork(accessToken, publicNetworkId, vmIds: List[str]):
    """
    公有网络挂载虚拟机
    :param accessToken:
    :param publicNetworkId: 公有网络UUID
    :param vmIds: 云主机UUID（数组格式）
    :return:
    """
    operationType = 5206
    data = _getOperationType(operationType, AsyncType.ASYNC.value)
    data['nid'] = publicNetworkId
    data['ids'] = vmIds

    return get_utils(accessToken).cloud_http_request.post(networkUrls.loadVMByPublicNetwork.value, data=data)


def unloadVMByPublicNetwork(accessToken, publicNetworkId, vmIds: List[str]):
    """
    公有网络卸载虚拟机
    :param accessToken:
    :param publicNetworkId: 公有网络UUID
    :param vmIds: 云主机UUID（数组格式）
    :return:
    """
    operationType = 5207
    data = _getOperationType(operationType, AsyncType.ASYNC.value)
    data['nid'] = publicNetworkId
    data['ids'] = vmIds

    return get_utils(accessToken).cloud_http_request.post(networkUrls.unloadVMByPublicNetwork.value, data=data)


def getL2NetworkByPublicNetwork(accessToken, **kwargs):
    """
    获取公有网络下的L2网络
    :param accessToken:
    :param kwargs: limit:
                   offset:
                   keyword: xxx
    :return:
    """
    return get_utils(accessToken).cloud_http_request.get(networkUrls.getL2NetworkByPublicNetwork.value,
                                                         {
                                                             'offset': kwargs.get('offset', OFFSET),
                                                             'limit': kwargs.get('limit', LIMIT),
                                                             'keyword': kwargs.get('keyword', ''),
                                                             'searchitem': 0,
                                                         })


def addPublicNetworkRange(accessToken, publicNetworkId, **kwargs):
    """
    添加公有网络IP段
    :param accessToken:
    :param publicNetworkId: 公有网络UUID
    :param kwargs: startIp: xxx
                   endIp: xxx
                   gateway: xxx
                   netmask: xxx
                   dns: xxx
                   domain: xxx
                   ipRangeUuid: xxx
    :return:
    """
    operationType = 5204
    data = _getOperationType(operationType, AsyncType.ASYNC.value)


def deletePublicNetworkRange(accessToken, publicNetworkId, segmentIds: list):
    """
    删除公有网络IP段
    :param accessToken:
    :param publicNetworkId: 公有网络UUID
    :param segmentIds: 三层网络段uuid(多个使用英文逗号分割)
    :return:
    """
    operationType = 5205
    data = _getOperationType(operationType, AsyncType.ASYNC.value)
    data['nid'] = publicNetworkId
    data['ids'] = segmentIds

    return get_utils(accessToken).cloud_http_request.post(networkUrls.deletePublicNetworkRange.value, data=data)


def addPublicNetworkDns(accessToken, publicNetworkId, dns, ipType):
    """
    添加公有网络DNS
    :param accessToken:
    :param publicNetworkId: 公有网络UUID
    :param dns: DNS
    :param ipType: 网络地址类型
                        1: IPV4
                        2: IPV6
    :return:
    """
    operationType = 5208
    data = _getOperationType(operationType, AsyncType.ASYNC.value)
    data['nid'] = publicNetworkId
    data['dns'] = dns
    data['iptype'] = ipType

    return get_utils(accessToken).cloud_http_request.post(networkUrls.addPublicNetworkDns.value, data=data)


def deletePublicNetworkDns(accessToken, publicNetworkId, dns):
    """
    删除公有网络DNS
    :param accessToken:
    :param publicNetworkId: 公有网络UUID
    :param dns: DNS(多个使用英文逗号分割)
    :return:
    """
    operationType = 5209
    data = _getOperationType(operationType, AsyncType.ASYNC.value)
    data['nid'] = publicNetworkId
    data['dnss'] = dns

    return get_utils(accessToken).cloud_http_request.post(networkUrls.deletePublicNetworkDns.value, data=data)


def getNetworkRangeByPublicNetwork(accessToken, publicNetworkId, **kwargs):
    """
    获取公有网络下的IP段
    :param accessToken:
    :param publicNetworkId: 公有网络UUID
    :param kwargs: limit:
                   offset:
                   keyword: xxx
                   searchItem: 1:IPV4;  2:IPV6; 3:双栈；
    :return:
    """
    return get_utils(accessToken).cloud_http_request.get(networkUrls.getNetworkRangeByPublicNetwork.value,
                                                         {
                                                             'offset': kwargs.get('offset', OFFSET),
                                                             'limit': kwargs.get('limit', LIMIT),
                                                             'keyword': kwargs.get('keyword', ''),
                                                             'nid': publicNetworkId,
                                                             'type': kwargs.get('keyword', 1)
                                                         })


def getDNSByPublicNetwork(accessToken, publicNetworkId, **kwargs):
    """
    通过公有网络id查询DNS
    :param accessToken:
    :param publicNetworkId: 公有网络UUID
    :param kwargs: limit:
                   offset:
    :return:
    """
    return get_utils(accessToken).cloud_http_request.get(networkUrls.getDNSByPublicNetwork.value,
                                                         {
                                                             'offset': kwargs.get('offset', OFFSET),
                                                             'limit': kwargs.get('limit', LIMIT),
                                                             'nid': publicNetworkId
                                                         })


def getVMByPublicNetwork(accessToken, publicNetworkId, **kwargs):
    """
    获取公有网络下的虚拟机
    :param accessToken:
    :param publicNetworkId: 公有网络UUID
    :param kwargs: limit:
                   offset:
                   keyword: xxx
    :return:
    """
    return get_utils(accessToken).cloud_http_request.get(networkUrls.getVMByPublicNetwork.value,
                                                         {
                                                             'offset': kwargs.get('offset', OFFSET),
                                                             'limit': kwargs.get('limit', LIMIT),
                                                             'nid': publicNetworkId
                                                         })


def getVMNotinPublicNetwork(accessToken, publicNetworkId, **kwargs):
    """
    获取公有网络未挂载的虚拟机
    :param accessToken:
    :param publicNetworkId: 公有网络UUID
    :param kwargs: limit:
                   offset:
    :return:
    """
    return get_utils(accessToken).cloud_http_request.get(networkUrls.getVMNotinPublicNetwork.value,
                                                         {
                                                             'offset': kwargs.get('offset', OFFSET),
                                                             'limit': kwargs.get('limit', LIMIT),
                                                             'nid': publicNetworkId
                                                         })
